package cn.lyd.pet.service.impl;


import cn.lyd.base.enm.OrderStateEnum;
import cn.lyd.base.enm.PetStateEnum;
import cn.lyd.base.enm.SearchMasterMsgStateEnum;
import cn.lyd.base.jwt.UserInfo;
import cn.lyd.base.service.impl.BaseServiceImpl;
import cn.lyd.base.until.*;
import cn.lyd.order.domain.OrderPetAcquisition;
import cn.lyd.order.mapper.OrderPetAcquisitionMapper;
import cn.lyd.org.domain.Shop;
import cn.lyd.org.mapper.ShopMapper;
import cn.lyd.pet.domain.Pet;
import cn.lyd.pet.domain.SearchMasterMsg;
import cn.lyd.pet.domain.SearchMasterMsgAuditLog;
import cn.lyd.pet.dto.AcceptDto;
import cn.lyd.pet.dto.SearchMasterMsgDto;
import cn.lyd.pet.mapper.PetMapper;
import cn.lyd.pet.mapper.SearchMasterMsgAuditLogMapper;
import cn.lyd.pet.mapper.SearchMasterMsgMapper;
import cn.lyd.pet.service.ISearchMasterMsgService;
import cn.lyd.product.domain.Product;
import cn.lyd.product.mapper.ProductMapper;
import cn.lyd.user.domain.Logininfo;
import cn.lyd.user.domain.User;
import cn.lyd.user.mapper.UserMapper;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author lyd
 * @since 2023-01-11
 */
@Service
public class SearchMasterMsgServiceImpl extends BaseServiceImpl<SearchMasterMsg> implements ISearchMasterMsgService {
    @Autowired
    private UserMapper userMapper;
    @Autowired
    private SearchMasterMsgAuditLogMapper auditLogMapper;
    @Autowired
    private ShopMapper shopMapper;
    @Autowired
    private PetMapper petMapper;
    @Autowired
    private SearchMasterMsgMapper msgMapper;
    @Autowired
    private OrderPetAcquisitionMapper orderMapper;
    @Autowired
    private ProductMapper productMapper;

    /**
     * 发布寻主
     * @param dto
     * @return
     */
    @Override
    public AjaxResult publish(SearchMasterMsgDto dto) {
        // 参数校验
        // 1.保存数据
        SearchMasterMsg masterMsg = new SearchMasterMsg();
        BeanUtils.copyProperties(dto,masterMsg);
        // 设置状态
        masterMsg.setState(SearchMasterMsgStateEnum.NO_AUDIT.getCode());
        // 设置userId 当心 空指针
//        Logininfo logininfo = LoginContext.getUserInfo().getLogininfo();
        UserInfo userInfo = ThreadLocalUtil.getUserInfo();
        Logininfo logininfo = userInfo.getLogininfo();
        // 通过logininfo的id查询userid
        User user = userMapper.findByLoginInfoId(logininfo.getId());
        masterMsg.setUser_id(user.getId());
        super.add(masterMsg);
        // 2.百度AI审核
        if (!BaiduAuditUtils.TextCensor(dto.getName())){
            // 审核不通过 添加日志
            SearchMasterMsgAuditLog auditLog = new SearchMasterMsgAuditLog();
            auditLog.setMsg_id(masterMsg.getId());
            auditLog.setAudit_time(new Date());
            auditLog.setNote("宠物名称审核不通过！"+dto.getName());
            auditLog.setAudit_id(-1L);
            auditLog.setState(SearchMasterMsgStateEnum.NO_AUDIT.getCode());
            // 添加审核日志
            auditLogMapper.add(auditLog);
            // msg的状态改成 审核失败
            masterMsg.setState(SearchMasterMsgStateEnum.AUDIT_FAIL.getCode());
            masterMsg.setNote("宠物名称审核不通过！:"+dto.getName());

        }else {
            // 审核通过
            // 3.找店铺
            Point point = DistanceUtil.getPoint(dto.getAddress());
            List<Shop> shopList = shopMapper.findAll();
            Shop nearestShop = DistanceUtil.getNearestShop(point, shopList);
            if (null != nearestShop){
                // 找到了店铺 我们应该干什么？
                masterMsg.setShop_id(nearestShop.getId());
            }

            // msg的状态改成 审核通过
            masterMsg.setState(SearchMasterMsgStateEnum.AUDIT_SUCCESS.getCode());
            masterMsg.setNote("审核通过");
        }
        // 4.最后 更新店铺状态
        super.update(masterMsg);

        return AjaxResult.me().success();
    }


    /**
     * 接单
     * @param dto dto.msgId=寻主消息的id
     */
    @Override
    public void accept(AcceptDto dto) {
        //1.参数校验 -注解实现

        //2.根据 dto.msgId查找寻主消息对象
        SearchMasterMsg msg = msgMapper.findById(dto.getMsgId());

        //2.生成收购单信息   // 现在的order 缺少一个petID
        OrderPetAcquisition order = new OrderPetAcquisition();
        order.setOrderSn(CodeGenerateUtils.generateOrderSn(msg.getUser_id()));
        order.setDigest(dto.getNote());
        order.setLastcomfirmtime(new Date(System.currentTimeMillis()+2*24*60*60*1000));
        order.setState(OrderStateEnum.ORDER.getCode());
        order.setPrice(msg.getPrice());
        order.setPaytype(1);
//        order.setPet_id();
        order.setUser_id(msg.getUser_id());
        order.setShop_id(msg.getShop_id());
        order.setAddress(msg.getAddress());
        order.setEmp_id(dto.getHandler());
        order.setSearch_master_msg_id(msg.getId());

        //3.生成宠物信息
        Pet pet = new Pet();
        pet.setName(msg.getName());
        pet.setCostprice(msg.getPrice());
        pet.setState(PetStateEnum.OFFSALE.getCode());
        pet.setType_id(msg.getPet_type());
        pet.setCreatetime(new Date());
        pet.setShop_id(msg.getShop_id());
        pet.setSearch_master_msg_id(msg.getId());


        //4.修改寻主消息的状态-已接单3
        msg.setState(SearchMasterMsgStateEnum.ORDER_SUCCESS.getCode());
        msgMapper.update(msg);

        petMapper.add(pet);
        // 添加order
        order.setPet_id(pet.getId());
        orderMapper.add(order);
    }



    /**
     * 拒单
     * @param id
     */
    @Override
    public void reject(Long id) {
        // 把当前消息 的shopId改成null
        msgMapper.reject(id);

    }

    /**
     * 发布服务
     * @param product_id 服务id
     * @return
     */
    @Override
    public AjaxResult sendServer(Long product_id) {

        //生成待处理信息 todo
        SearchMasterMsg msg = new SearchMasterMsg();

        //找到服务和店铺
        Product product = productMapper.findById(product_id);
        Long shop_id = product.getShop_id();
        Shop shop = shopMapper.findById(shop_id);



        //添加服务消息
//        msgMapper.add();


        return AjaxResult.me().success();
    }


}
